﻿using System.Text;

namespace LibraryGuard.Internals
{
	/// <summary>
	/// Default functions.
	/// </summary>
	internal static class DefaultFunctions
	{
		/// <summary>
		/// Trims selected characters from start and end.
		/// </summary>
		/// <param name="data"></param>
		/// <param name="chars"></param>
		/// <returns></returns>
		public static string Trim(string data, string chars)
		{
			return data.Trim(chars.ToCharArray());
		}

		/// <summary>
		/// Trims selected characters from start.
		/// </summary>
		/// <param name="data"></param>
		/// <param name="chars"></param>
		/// <returns></returns>
		public static string TrimStart(string data, string chars)
		{
			return data.TrimStart(chars.ToCharArray());
		}

		/// <summary>
		/// Trims selected characters from end.
		/// </summary>
		/// <param name="data"></param>
		/// <param name="chars"></param>
		/// <returns></returns>
		public static string TrimEnd(string data, string chars)
		{
			return data.TrimEnd(chars.ToCharArray());
		}

		/// <summary>
		/// Pads string with specified character from left.
		/// </summary>
		/// <param name="data"></param>
		/// <param name="str"></param>
		/// <param name="length"></param>
		/// <returns></returns>
		public static string PadLeft(string data, string str, int length)
		{
			char c = str[0];
			if (data.Length < length)
				data = new string(c, length - data.Length) + data;
			return data;
		}

		/// <summary>
		/// Pads string with specifiet character from right.
		/// </summary>
		/// <param name="data"></param>
		/// <param name="str"></param>
		/// <param name="length"></param>
		/// <returns></returns>
		public static string PadRight(string data, string str, int length)
		{
			char c = str[0];
			if (data.Length < length)
				data = data + new string(c, length - data.Length);
			return data;
		}

		/// <summary>
		/// Pads string with specifiet character from both left and right side.
		/// </summary>
		/// <param name="data"></param>
		/// <param name="str"></param>
		/// <param name="length"></param>
		/// <returns></returns>
		public static string Pad(string data, string str, int length)
		{
			char c = str[0];
			if (data.Length < length)
				data = new string(c, (int)((length - data.Length) / 2.0 + 0.5)) + data + new string(c, (length - data.Length) / 2);
			return data;
		}

		/// <summary>
		/// Replaces invalid filename characters in string.
		/// </summary>
		/// <param name="str"></param>
		/// <returns></returns>
		public static string SafeName(string str)
		{
			StringBuilder builder = new StringBuilder(str);
			builder.Replace('/', '-'); //Special case
			var invalidChars =System.IO.Path.GetInvalidFileNameChars();
			foreach (var c in invalidChars)
				builder.Replace(c.ToString(), "");
			return builder.ToString();
		}
	}
}
